
$(function(){

	// 基本数据
	loadCount();
	// 通行记录
	loadRecord();
	// 通行记录队列执行
	timeQueueLoad();

	// 加载大屏名称
	$.post(ctxPath+'data/face/selectDataName', {}, function(result) {
		if(result.success){
			$(".text_1").text(result.data || "人脸抓拍分析数据大屏");
		}
	})

	// 两个钟刷新页面
	setTimeout(function () {
		if(judgeTemp == 1){
			window.location.reload();
		}
	}, 1000*60*60*2)


})


/**
 * -----------------------------------------------------------------------------------------------------------------------------------
 * 【基本数据】-接口
 */
function loadCount(){
	$.post(ctxPath+'data/face/selectAttendCount', {}, function(result) {
		if(result.success){
			var obj = result.data;
			var avg = parseFloat(obj.attendCount / obj.sum * 100).toFixed(2);
			avg = avg == 100 ? parseInt(avg) : avg;
			avg = parseInt(avg) == 0 ? 0 : avg;
			$(".text_46").text(addCommas(obj.sum));
			$(".text_47").text((avg == "NaN" ? 0 : avg)+"%");
			$(".text_49").html(simplifyNum(obj.timeCount));
			$(".text_52").text(addCommas(obj.count));
			$(".text_54").text(addCommas(obj.attendCount));
		}
	})
}

var lastObj, lastRecordState;
/**
 * -----------------------------------------------------------------------------------------------------------------------------------
 * 【通行记录】-接口
 */
function loadRecord(){
	if(!lastRecordState){
		lastRecordState = true;
		$.post(ctxPath+'data/face/selectScan8', lastObj, function(result) {
			if(result.success){
				var list = result.data;
				if(list && list.length > 0){
					lastObj = list[list.length-1];
					tempRecordList.push(list);
				}
			}
			lastRecordState = false;
		})
	}
}

/**
 * -----------------------------------------------------------------------------------------------------------------------------------
 * 将需要显示的通行记录添加到队列中-防止一直滚动
 */
var tempRecordList = [];
function timeQueueLoad() {
	setInterval(function () {
		if (!tempRecordState && tempRecordList.length > 0){
			tempRecordState = true
			loadRecordList(tempRecordList[0]);
		}
	}, 300);
}

var tempInterRecord, tempRecordState, tempExistsList = [];
/**
 * -----------------------------------------------------------------------------------------------------------------------------------
 * 删除记录中前面的，再渲染新的
 */
function loadRecordList(list){
	clearInterval(tempInterRecord);
	// 获取累加渲染前的标签长度
	var len = $("#record_list").find(".list_box").length;
	var tempLen = 0;
	// 添加到末端
	for(var i=0; i<list.length; i++){
		var obj = list[i];
		if(existsBox(obj.id) == -1){
			showRecord(obj);
			tempExistsList.push(obj);
			tempLen ++;
		}
	}
	// 计算需要动画删除和显示的
	var leftCount = 0 - (6 * tempLen);
	var tempCount = 0;
	var timeNum = 10 - tempLen;
	// 临时数组中的记录渲染后删除
	tempRecordList.splice(0, 1);
	// 保存最后一条最新记录，后台根据这个对象的范围开始往后面查最新的
	var lenSum = len + tempLen;
	if(lenSum > 8){
		var num = (lenSum-8);
		leftCount = 0 - (6 * (lenSum-8));
		// 开始动画
		tempInterRecord = setInterval(function(){
			if(tempCount < leftCount){
				clearInterval(tempInterRecord);
				for(var i=0; i<tempLen; i++){
					if(num > 0){
						num --;
						$("#record_list").find(".list_box").eq(0).remove();
						tempExistsList.splice(0, 1); // 用来判断页面显示是否重复
					}
				}
				tempRecordState = false;
			}else if(len > 0){
				tempCount = tempCount - 0.1;
				$("#record_list").find(".list_box").eq(0).css("margin-left", tempCount+"rem");			
			}
		}, timeNum)
	}else {
		tempRecordState = false;
	}
}

/**
 * -----------------------------------------------------------------------------------------------------------------------------------
 * 判断是否重复记录，重复不显示
 * @param id
 */
function existsBox(id) {
	for (var i=0; i<tempExistsList.length; i++){
		var obj = tempExistsList[i];
		if (obj.id == id){
			return 1;
		}
	}
	return -1;
}


/**
 * -----------------------------------------------------------------------------------------------------------------------------------
 * 渲染单条记录
 */
function showRecord(obj){
	var nameClass = "";
	if(obj.name && obj.name.length > 3){
		nameClass = "letter-spacing: 1px;";
	}
	obj.name = obj.name || '访客';
	var s = '<div class="list_box" lay-id="'+obj.id+'">';
	s += '	<img class="list_bg_img" src="'+ctxPath+'images/data/face/SketchPngd3699c274ecd3416f54007d90ad5a9a568452819fb1af432b35a72981de0f10e.png"/>';
	s += '	<div class="list_bg_text">';
	s += '		<img class="list_face_img" src="'+ctxPath+obj.scanImage+'"/>';
	s += '		<div class="list_head_bg"></div>';
	s += '		<div class="list_head_box">';
	s += '			<span class="list_head_text_l">'+obj.scanTime+'</span>';
	s += '			<span class="list_head_text_r">'+(obj.inOut == 1? "进": "出")+'</span>';
	s += '		</div>';
	s += '		<div>';
	s += '			<img class="list_bottom_img" src="'+ctxPath+'images/data/face/SketchPngae5043aa906f8ce747b81178e0ee88b774d17806e69f125877ce992e2d96b096.png"/>';
	s += '			<div class="list_bottom_num">'+parseInt(obj.checkFaceNum)+'%</div>';
	s += '			<div class="list_bottom_label">相似度</div>';
	s += '			<span class="list_bottom_name" style="'+nameClass+'">'+(obj.eno == 'stranger'? "陌生人": obj.name)+'</span>';
	s += '		</div>';
	s += '	</div>';
	s += '</div>';
	$("#record_list").append(s);		
}


/**
 * -----------------------------------------------------------------------------------------------------------------------------------
 * 当前时间
 */
function getSysDate(){
	var time = new Date();
	var month = ((time.getMonth()+1)+"").length < 2?"0"+(time.getMonth()+1):(time.getMonth()+1);
	var dates = (time.getDate()+"").length < 2?"0"+time.getDate():time.getDate();
	var minute = (time.getMinutes()+"").length < 2?"0"+time.getMinutes():time.getMinutes();
	var hours = (time.getHours()+"").length < 2?"0"+time.getHours():time.getHours();
	var minute = (time.getMinutes()+"").length < 2?"0"+time.getMinutes():time.getMinutes();
	var second = (time.getSeconds()+"").length < 2?"0"+time.getSeconds():time.getSeconds();
	$(".text_2").text(hours+":"+minute+":"+second);
	$(".text_3").text(time.getFullYear()+"-"+month+"-"+dates);
	$(".text_4").text(getWeek(time));
}
function getWeek(date){
	var week = date.getDay();
	var arr = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
	return arr[week];
}
getSysDate();
setInterval("getSysDate()", 1000);

/**
 * 数字超过三位逗号隔开
 * @param {Object} nStr
 */
function addCommas(nStr) {
	nStr += '';
	var x = nStr.split('.');
	var x1 = x[0];
	var x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

/**
 * 返回万、百万、千万、亿等单位
 * @param {Object} number
 */
function simplifyNum(number) {
	if (!number && number != 0) return number;
	var str_num
	if (number >= 1E3 && number < 1E4) {
		//str_num = number / 1E3
		//return str_num + '千'
		return number;
	} else if (number >= 1E4 && number < 1E6) {
		str_num = number / 1E4
		return str_num + '<span style="font-size: 1rem;">万</span>';
	} else if (number >= 1E6 && number < 1E7) {
		str_num = number / 1E6
		return str_num + '<span style="font-size: 0.5rem;">百万</span>'
	} else if (number >= 1E7 && number < 1E8) {
		str_num = number / 1E7
		return str_num + '<span style="font-size: 0.5rem;">千万</span>'
	} else if (number >= 1E8 && number < 1E10) {
		str_num = number / 1E8
		return str_num + '<span style="font-size: 0.5rem;">亿</span>'
	} else if (number >= 1E10 && number < 1E11) {
		str_num = number / 1E10
		return str_num + '<span style="font-size: 0.5rem;">百亿</span>'
	} else if (number >= 1E11 && number < 1E12) {
		str_num = number / 1E11
		return str_num + '<span style="font-size: 0.5rem;">千亿</span>'
	} else if (number >= 1E12) {
		str_num = number / 1E12
		return str_num + '<span style="font-size: 0.5rem;">万亿</span>'
	} else { //一千以下
		return number
	}
}



